﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eStock.hControl;
using HCommon;

namespace eStock
{
    public partial class frmUser : hForm
    {
        public frmUser()
        {
            InitializeComponent();
        }

        private void LoadGrid()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = Dao.Get_Table("Exec spUser_Q '" + Common.xoaNhay(txtUser.Text) + "','" + cboGroup.SelectedValue.ToString() + "','" + cboDept.SelectedValue.ToString() + "'");
                grdUser.DataSource = dt;
            }
            catch (Exception ex)
            {
                Common.GhiLog("LoadGrid", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void frmUser_Load(object sender, EventArgs e)
        {
            LoadGrid();
        }

        private void grdUser_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (grdUser.Rows.Count == 0 || e.RowIndex < 0)
                {
                    return;
                }
                frmUser_Add UserAdd = new frmUser_Add();
                UserAdd.UpdateKey = grdUser.Rows[e.RowIndex].Cells["user_name"].Value.ToString();
                UserAdd.FormMode = Mode.Update;
                UserAdd.ShowDialog();
                if (UserAdd != null)
                {
                    LoadGrid();
                }
            }
            catch (Exception ex)
            {
                Common.GhiLog("grdUser_CellDoubleClick", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void btnQry_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            try
            {
                frmUser_Add UserAdd = new frmUser_Add();
                UserAdd.FormMode = Mode.Insert;
                UserAdd.ShowDialog();
                if (UserAdd != null)
                {
                    LoadGrid();
                }
            }
            catch (Exception ex)
            {
                Common.GhiLog("btnNew_Click", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grdUser_hDelete(object sender, EventArgs e)
        {
            try
            {
                int delCnt = 0;
                foreach (DataGridViewRow row in grdUser.SelectedRows)
                {
                    if (tblUSER.Delete(row.Cells["user_name"].Value.ToString()) > 0)
                    {
                        grdUser.Rows.Remove(row);
                        delCnt++;
                    }
                }
                Dialog.ShowMess(Dialog.mType.ok, "Đã xóa thành công " + delCnt + " bản ghi.");
            }
            catch (Exception ex)
            {
                Common.GhiLog("grdUser_hDelete", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }
    }
}
